<div style="margin-top: 20px;">
    <?php echo $this->Form->create('Quyen'); ?>      
    <div style="margin-left: 30px;"><?php echo $this->element('message'); ?></div>
    <div align="center">        
        <table width="700" border="0" cellpadding="5">
            <tr>
                <td colspan="5"><div align="center"><span style="font-size: 18px;font-weight: bold;">PHÂN NGƯỜI DÙNG VÀO NHÓM</span></div></td>
            </tr>
            <tr>
              <td width="47">Nhóm </td>
              <td width="261">
                  <?php 
                        echo $this->Form->input('nhomid', array(
                                    'options' => $arr_Nhom,   
                                    'label' => false,
                                    'style' =>  'width:250px;',
                                    'empty' => '---- Chọn nhóm ----'                                    
                                ));
                    ?>   
              </td>
              <td width="57">Phân hệ</td>
              <td width="174">
                  <?php 
                        echo $this->Form->input('menuid', array(
                                    'options' => $arr_Menu_level0,   
                                    'label' => false,
                                    'style' =>  'width:180px;',
                                    'empty' => '---- Chọn phân hệ ----',
                                    'onchange' => 'loadphanhe( QuyenNhomid.value ,this.value)'
                                ));
                    ?>   
              </td>
              <td width="49"><input type="submit" style="width:50px" value="Lưu"/></td>
            </tr>  
        </table>          
        <article class="module width_3_quarter" style="width: 700px;">
            <header>
                <div class="wrapper_add">                     
                </div>
                <div class="show_record">            					
                </div>
            </header>
            <div class="tab_container">
                <div id="tab1" class="tab_content">
                    <?php if( !isset($session) ){ ?>
                    <table class="tablesorter" cellspacing="0" style="width:700px;"> 
                        <thead> 
                            <tr> 
                                <th width="39">STT</th>
                                <th width="607">Tiêu đề menu</th>
                                <th width="30">Thêm</th>
                                <th width="30">Sửa</th>
                                <th width="30">Xóa</th>                 
                            </tr> 						
                        </thead>                          
                    </table> 
                    <?php }else{ ?>
                    <table class="tablesorter" cellspacing="0" style="width:700px;"> 
                        <thead> 
                            <tr> 
                                <th width="39">STT</th>
                                <th width="607">Tiêu đề menu</th>
                                <th width="30">
                                    Thêm
                                    <input type="checkbox" name="viewall" id="checkviewall" style="margin-left: 12px;"/>
                                </th>
                                <th width="30" align="center">
                                    Sửa
                                    <input type="checkbox" name="editall" id="checkeditall"/>
                                </th>
                                <th width="30" align="center">
                                    Xóa
                                    <input type="checkbox" name="deleteall" id="checkdeleteall"/>
                                </th>                                
                            </tr> 						
                        </thead> 
                        <tbody> 
                            <?php 
                                $i = 1;            
                                foreach( $arr_Menu as $value ) {                
                            ?>
                            <tr>            
                                <td style="text-align: center"><?php echo $i; ?></td>
                                <td><?php echo $value['Menu']['tenmenu']; ?></td>
                                <?php 
                                    $check = true;
                                    foreach( $arr_Quyen as $value1 ){ 
                                        if( $value['Menu']['id'] == $value1['Quyen']['menuid'] ){
                                            $check = false;
                                    ?>
                                <td><input type="checkbox" name="data[Quyen][save][<?php echo $value['Menu']['id']; ?>]" class="add" value="<?php echo $value['Menu']['id']; ?>" <?php if( $value1['Quyen']['xem'] == 1 ) echo 'checked';?> /></td>
                                <td><input type="checkbox" name="data[Quyen][edit][<?php echo $value['Menu']['id']; ?>]" class="edit" value="<?php echo $value['Menu']['id']; ?>" <?php if( $value1['Quyen']['sua'] == 1 ) echo 'checked';?>/></td>
                                <td><input type="checkbox" name="data[Quyen][delete][<?php echo $value['Menu']['id']; ?>]" class="delete" value="<?php echo $value['Menu']['id']; ?>" <?php if( $value1['Quyen']['xoa'] == 1 ) echo 'checked';?>/></td>
                                <?php }}
                                    if($check){
                                     ?>
                                     <td><input type="checkbox" name="data[Quyen][save][<?php echo $value['Menu']['id']; ?>]" class="add" value="<?php echo $value['Menu']['id']; ?>" /></td>
                                    <td><input type="checkbox" name="data[Quyen][edit][<?php echo $value['Menu']['id']; ?>]" class="edit" value="<?php echo $value['Menu']['id']; ?>" /></td>
                                    <td><input type="checkbox" name="data[Quyen][delete][<?php echo $value['Menu']['id']; ?>]" class="delete" value="<?php echo $value['Menu']['id']; ?>" /></td>
                                <?php
                                    }   
                                ?>
                            </tr>        
                            <?php 
                                $i++;
                                }
                            ?>
                        </tbody> 
                    </table> 
                    <script type="text/javascript">
                    $("#checkviewall").click(function () {
                              $('.add').attr('checked', this.checked);
                    });
                    $(".add").click(function(){ 
                        if($(".add").length == $(".add:checked").length) {
                            $("#checkviewall").attr("checked", "checked");
                        } else {
                            $("#checkviewall").removeAttr("checked");
                        }

                    });
                    $("#checkeditall").click(function () {
                              $('.edit').attr('checked', this.checked);
                    });
                    $(".edit").click(function(){ 
                        if($(".edit").length == $(".edit:checked").length) {
                            $("#checkeditall").attr("checked", "checked");
                        } else {
                            $("#checkeditall").removeAttr("checked");
                        }

                    });
                    $("#checkdeleteall").click(function () {
                              $('.delete').attr('checked', this.checked);
                    });
                    $(".delete").click(function(){ 
                        if($(".delete").length == $(".delete:checked").length) {
                            $("#checkdeleteall").attr("checked", "checked");
                        } else {
                            $("#checkdeleteall").removeAttr("checked");
                        }

                    });
                    </script>
                    <?php }?>
                </div><!-- end of #tab1 -->
            </div><!-- end of .tab_container -->
        </article>
    </div>
    <?php echo $this->Form->end(); ?>
</div>
<script>
function loadphanhe(inputString1, inputString2){ 
    $.ajax({
        url:"/admin/quyens/loadphanhe/" + inputString1 + "/" + inputString2,
        beforeSend: function() {
            $('.tablesorter').html("<img src='/img/loading.gif' />");
        },
        success:function(html){                
            $(".tablesorter").html(html);
        }
    }); 
}
</script>